import os

import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from web_ui_item.pageobject.base_page import BasePage
from web_ui_item.pageobject.user_group_page import UserGroupPage
from web_ui_item.utils.logger import logger
from web_ui_item.utils.read_data import r_data

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
ELE_PATH = os.path.join(BASE_PATH, "data/elements.yaml")

class ManagementPage(BasePage):
    _page = r_data.load_yaml(ELE_PATH)["management_page"]

    def goto_user_group(self):
        '''
        进入用户组管理页面
        :return:
        '''
        # 获取打开的多个窗口句柄
        windows = self.driver.window_handles
        # 切换到当前最新打开的窗口
        self.driver.switch_to.window(windows[-1])
        WebDriverWait(self.driver, 20).until(
            expected_conditions.element_to_be_clickable(
                (By.CSS_SELECTOR, self._page['tips_close_btn'])))
        with allure.step("打开用户组管理页面"):
            self.find(By.CSS_SELECTOR, self._page['tips_close_btn']).click()
            self.find(By.CSS_SELECTOR, self._page['organization']).click()
            self.find(By.CSS_SELECTOR, self._page['user_group_manage']).click()
        logger.debug('跳转到用户组管理页面')
        return UserGroupPage(self.driver)

    def goto_department(self):
        pass
